package com.edu.exam.entity.examination.control;

import com.edu.exam.enums.examination.NavigationMode;
import com.edu.exam.enums.examination.NavigationType;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;

import java.time.LocalDateTime;

/**
 * 导航控制实体类
 * 所属模块：E-01 考试执行模块
 * 对应数据库表：navigation_control
 *
 * <p>导航控制实体用于管理答题过程中的导航控制，包括题目切换、导航历史、
 * 导航约束、导航权限等功能。支持多种导航模式和详细的导航过程跟踪。</p>
 *
 * <p>核心功能：
 * - 导航管理：管理题目间的导航切换
 * - 历史记录：记录详细的导航历史
 * - 权限控制：控制导航权限和约束
 * - 模式支持：支持多种导航模式
 * - 导航统计：提供导航行为统计分析</p>
 *
 * @author System
 * @version 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
@Entity
public class NavigationControl {

    /**
     * 主键ID
     */
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    /**
     * 导航ID
     *
     * <p>导航控制的唯一标识符，采用UUID格式生成。</p>
     */
    @Column(name = "navigation_id", nullable = false, length = 32, unique = true)
    private String navigationId;

    /**
     * 会话ID
     *
     * <p>关联的答题控制会话ID。</p>
     */
    @Column(name = "session_id", nullable = false, length = 32)
    private String sessionId;

    /**
     * 学生ID
     *
     * <p>答题学生的ID。</p>
     */
    @Column(name = "student_id", nullable = false, length = 32)
    private String studentId;

    /**
     * 当前题目ID
     *
     * <p>学生当前所在的题目ID。</p>
     */
    @Column(name = "current_question_id", nullable = false, length = 32)
    private String currentQuestionId;

    /**
     * 上一题目ID
     *
     * <p>上一个访问的题目ID。</p>
     */
    @Column(name = "previous_question_id", length = 32)
    private String previousQuestionId;

    /**
     * 导航模式
     *
     * <p>答题导航的模式。
     * 支持的模式：SEQUENTIAL（顺序导航）、RANDOM（随机导航）、FREE（自由导航）、RESTRICTED（受限导航）</p>
     */
    @Enumerated(EnumType.STRING)
    @Column(name = "navigation_mode", nullable = false, length = 20)
    private NavigationMode navigationMode = NavigationMode.SEQUENTIAL;

    /**
     * 导航权限
     *
     * <p>JSON格式的导航权限配置。</p>
     */
    @Column(name = "navigation_permissions", columnDefinition = "TEXT")
    private String navigationPermissions;

    /**
     * 导航约束
     *
     * <p>JSON格式的导航约束配置。</p>
     */
    @Column(name = "navigation_constraints", columnDefinition = "TEXT")
    private String navigationConstraints;

    /**
     * 导航历史
     *
     * <p>JSON格式的导航历史记录。</p>
     */
    @Column(name = "navigation_history", columnDefinition = "LONGTEXT")
    private String navigationHistory;

    /**
     * 导航统计
     *
     * <p>JSON格式的导航统计数据。</p>
     */
    @Column(name = "navigation_statistics", columnDefinition = "TEXT")
    private String navigationStatistics;

    /**
     * 导航设置
     *
     * <p>JSON格式的导航设置参数。</p>
     */
    @Column(name = "navigation_settings", columnDefinition = "TEXT")
    private String navigationSettings;

    /**
     * 总导航次数
     *
     * <p>总的导航次数。</p>
     */
    @Column(name = "total_navigation_count", nullable = false)
    private Integer totalNavigationCount = 0;

    /**
     * 前进导航次数
     *
     * <p>向前导航的次数。</p>
     */
    @Column(name = "forward_navigation_count", nullable = false)
    private Integer forwardNavigationCount = 0;

    /**
     * 后退导航次数
     *
     * <p>向后导航的次数。</p>
     */
    @Column(name = "backward_navigation_count", nullable = false)
    private Integer backwardNavigationCount = 0;

    /**
     * 跳转导航次数
     *
     * <p>跳转导航的次数。</p>
     */
    @Column(name = "jump_navigation_count", nullable = false)
    private Integer jumpNavigationCount = 0;

    /**
     * 是否允许前进
     *
     * <p>标记是否允许向前导航。</p>
     */
    @Column(name = "allow_forward")
    private Boolean allowForward = true;

    /**
     * 是否允许后退
     *
     * <p>标记是否允许向后导航。</p>
     */
    @Column(name = "allow_backward")
    private Boolean allowBackward = true;

    /**
     * 是否允许跳转
     *
     * <p>标记是否允许题目跳转。</p>
     */
    @Column(name = "allow_jump")
    private Boolean allowJump = false;

    /**
     * 是否允许复习
     *
     * <p>标记是否允许复习已答题目。</p>
     */
    @Column(name = "allow_review")
    private Boolean allowReview = true;

    /**
     * 最后导航时间
     *
     * <p>最后导航的时间。</p>
     */
    @Column(name = "last_navigation_time")
    private LocalDateTime lastNavigationTime;

    /**
     * 导航异常计数
     *
     * <p>导航异常的次数。</p>
     */
    @Column(name = "navigation_error_count")
    private Integer navigationErrorCount = 0;

    /**
     * 导航违规计数
     *
     * <p>导航违规的次数。</p>
     */
    @Column(name = "navigation_violation_count")
    private Integer navigationViolationCount = 0;

    /**
     * 导航标签
     *
     * <p>JSON格式的导航标签。</p>
     */
    @Column(name = "navigation_tags", columnDefinition = "TEXT")
    private String navigationTags;

    /**
     * 导航备注
     *
     * <p>导航的备注信息。</p>
     */
    @Column(name = "navigation_notes", columnDefinition = "TEXT")
    private String navigationNotes;

    // ===== 审计字段 =====

    /**
     * 创建时间
     */
    @CreationTimestamp
    @Column(name = "created_time", nullable = false, updatable = false)
    private LocalDateTime createdTime;

    /**
     * 更新时间
     */
    @UpdateTimestamp
    @Column(name = "updated_time", nullable = false)
    private LocalDateTime updatedTime;

    /**
     * 版本号（乐观锁）
     */
    @Version
    @Column(name = "version")
    private Integer version = 0;

    /**
     * 逻辑删除标记
     */
    @Column(name = "is_deleted", nullable = false)
    private Boolean isDeleted = false;
}